package com.atguigu.custmer.view;

import com.atguigu.custmer.javabean.Customer;
import com.atguigu.custmer.service.CustomerService;
import com.atguigu.custmer.tool.CMUtility;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/11 20:23
 * @注释
 */
public class CustomerView {

    CustomerService service = new CustomerService(5);

    public static void main(String[] args) {
        CustomerView cv = new CustomerView();
        cv.enterMainMenu();
    }

    public void enterMainMenu() {
        boolean loopFlag = true;
        while (loopFlag) {
            System.out.print("""
                                 -----------------拼电商客户管理系统-----------------
                                    
                                                    1 添 加 客 户
                                                    2 修 改 客 户
                                                    3 删 除 客 户
                                                    4 客 户 列 表
                                                    5 退       出
                               
                                                    请选择(1-5): 
                    """);
            char c = CMUtility.readChar();
            switch (c) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.println("确认退出吗？(Y/N): ");
                    char select = CMUtility.readConfirmSelection();
                    if (select == 'Y' || select == 'y') {
                        System.out.println("欢迎下次光临！");
                        break;
                    }
            }

        }

    }

    /**
     * 添加新客户
     */
    public void addNewCustomer() {
        System.out.println("----------------------添加客户-----------------------");
        System.out.print("姓名：");
        String name = CMUtility.readString(10);
        System.out.print("性别：");
        char gender = CMUtility.readChar();
        System.out.print("年龄：");
        int age = CMUtility.readInt();
        System.out.print("电话：");
        String phone = CMUtility.readString(11);
        System.out.print("邮箱：");
        String emile = CMUtility.readString(20);
        //对数据进行封装
        Customer customer = new Customer(name, gender, age, phone, emile);
        //将数据传送给后台
        boolean b = service.addCustomer(customer);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    /**
     * 修改客户信息
     */
    public void modifyCustomer() {
        System.out.println("-------------------修改客户信息--------------------");
        System.out.print("请输入待修改客户的编号(输-1退出): ");
        int id = CMUtility.readInt();
        if (id == -1) {
            return;
        }
        Customer customer = service.getCustomer(id - 1);
        System.out.print("姓名(" + customer.getName() + ")：");
        String name = CMUtility.readString(10, customer.getName());
        System.out.print("性别(" + customer.getGender() + ")：");
        char gender = CMUtility.readChar(customer.getGender());
        System.out.print("年龄(" + customer.getAge() + ")：");
        int age = CMUtility.readInt(customer.getAge());
        System.out.print("电话(" + customer.getPhone() + ")：");
        String phone = CMUtility.readString(11, customer.getPhone());
        System.out.print("邮箱(" + customer.getEmail() + ")：");
        String emile = CMUtility.readString(20, customer.getEmail());
        //对数据进行封装
        Customer customer1 = new Customer(name, gender, age, phone, emile);
        //将数据传送到后端
        boolean b = service.replaceCustomer(id - 1, customer1);
        if (b) {
            System.out.println("修改完成");
        } else {
            System.out.println("修改失败");
        }
        System.out.println("----------------修改完毕----------------------");
    }

    /**
     * 删除客户信息
     */
    public void deleteCustomer() {
        System.out.println("---------------删除客户------------------------");
        System.out.println("请选择待删除客户编号(-1退出)：");
        int id = CMUtility.readInt();

        if (id == -1) {
            return;
        }
        System.out.println("请确认是否删除(Y/N)：");
        char c = CMUtility.readConfirmSelection();
        if (c == 'Y' || c == 'y') {
            boolean b = service.deleteCustomer(id - 1);
            if (b) {
                System.out.println("删除完成");
            } else {
                System.out.println("删除失败");
            }
        }
        System.out.println("----------------删除完成-----------------------");

    }

    /**
     * 客户列表
     */
    public void listAllCustomer() {
        System.out.println("----------------客户列表---------------------");
        System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t\t邮箱");
        Customer[] customer = service.getAllCustomer();
        for (int i = 0; i < customer.length; i++) {
            System.out.println(i + 1 + "\t" + customer[i]);
        }
        System.out.println("---------------客户列表完成----------------------");
    }
}
